/*
 * @Author: your name
 * @Date: 2020-11-08 10:21:24
 * @LastEditTime: 2021-05-20 20:43:57
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: /applet_ynlj_v1.0.0/service/base_service.js
 */
// import { values } from "../libs/runtime";
// import { openType } from "../components/vant-weapp/mixins/open-type";
// 对wx.request的基本Promise封装

// 正式地址
//测试的
let URL = 'https://jb.shubowei.com/test-api';
//正式服
//let URL = 'https://mini.jiabao19.com/front-server'
// 本地
//let URL = 'http://zao.mynatapp.cc'
//let URL = 'http://192.168.254.218:20610'
//普通静态图片用这个域名
const IMAGE_URL = 'https://images.jiabao19.com/images/';

//对应公司的上传图片用这个域名
const NEWIMAGE_URL = "https://images.jiabao19.com/images/"

const FIRST_LOGIN_HOME = "fistLoginHome"
const BIND_USER_ID = "bindUserId"
const SERVICE_URL = "https://work.weixin.qq.com/kfid/kfcec73748adbb38459"
const SERVICE_ID = "wwbe340c9c938d577a"
// imTODO
const IMAPP_ID = "YJY272218209";
// imTODO IM请求地址:
const IMURL = 'https://im.service.ylyc.top';
const wsApi = "wss://ws.im.ylyc.top/chat";
const VIDEO_URL = '?vframe/jpg/offset/1';
const CHECK_SUCCESS = "https://saas.img.ylyc.top/icon/shsuccess_icon.png";
const CHECK_FAIL = "https://saas.img.ylyc.top/icon/shfail_icon.png";
const CHECK_PROGRESS = "https://saas.img.ylyc.top/icon/shprogress_icon.png";
const MAP_KEY = "5ZOBZ-OLBW2-ZTIUN-CHREB-3NY7Q-HFF3K";
const VIDEO_COVER = "?x-oss-process=video/snapshot,t_10000,m_fast"

const getEvnInfo = () => {
  console.log("JJLog:这里调用了=====")
  const accountInfo = wx.getAccountInfoSync();
  console.log("JJLog:这里返回信息是：" + JSON.stringify(accountInfo));
  const envVersion = accountInfo.miniProgram.envVersion;
  const baseUrl = {
    develop: "https://yjy.ylyc.top/prod-api/1534541823864795137/wxapp",
    trial: "https://yjy.ylyc.top/prod-api/1534541823864795137/wxapp",
    release: "https://yjy.ylyc.top/prod-api/1534541823864795137/wxapp",
  }
  URL = baseUrl[envVersion];
  console.log("JJLog:请求URL是:" + URL);
}
//getEvnInfo();

const base = (url, method = 'GET', params = {}, isIm = false, isForm = false) => {
  let api = `${URL}${url}`
  if (isIm) {
    api = `${IMURL}${url}`
  }
  return new Promise((resolve, reject) => {
    let httpConfig = {
      url: api,
      data: params,
      method: method,
      success: (res) => {
        if (res.statusCode === 200) {
          resolve(res.data)
        } else {
          reject({
            isSuccess: false,
            result: res.data
          })
        }
      },
      fail() {
        reject({
          isSuccess: false,
          result: {
            message: '网络错误'
          }
        })
      }
    }

    let cookie = null;
    let openId = null;
    try {
      openId = wx.getStorageSync('openId');
      //openId = '051a86adfd674ea5baf395d7043cdc38' //教授的号
      //openId = '92037d167ad440109eb0670e9ead5a7f' //舒老师的号，测试服
      // openId = 'c9109fd4ca0a428a865e8385557232cc'
      // console.log("JJLog:openId=" + openId);
      if (isIm) {
        openId = wx.getStorageSync('imToken');
      }
    } catch (e) { }
    if (openId) {
      // httpConfig = Object.assign(httpConfig, { header: { 'Cookie': cookie } })
      //新的传值逻辑
      let header = {
        'Authori-Zation': openId
      }
      if (isForm) {
        header['content-type'] = 'application/x-www-form-urlencoded'
      }
      if (isIm) {
        header.appId = IMAPP_ID
      }
      httpConfig = Object.assign(httpConfig, {
        header
      })
    }
    wx.request(httpConfig)
  })
}


const get = (url, params = {}, isIm = false) => {
  return base(url, 'GET', params, isIm)
}

const post = (url, params = {}, isIm = false, isForm = false) => {
  return base(url, 'POST', params, isIm, isForm)
}

const del = (url, params = {}) => {
  return base(url, 'DELETE', params)
}

const put = (url, params = {}) => {
  return base(url, 'PUT', params)
}


module.exports = {
  get,
  post,
  del,
  put,
  URL,
  IMAGE_URL,
  NEWIMAGE_URL,
  VIDEO_URL,
  CHECK_SUCCESS,
  CHECK_FAIL,
  CHECK_PROGRESS,
  MAP_KEY,
  IMAPP_ID,
  wsApi,
  FIRST_LOGIN_HOME,
  BIND_USER_ID,
  SERVICE_URL,
  SERVICE_ID,
  VIDEO_COVER
}